package com.framework.web.bus.service;
import com.framework.web.bus.domain.GoodsInfo;
import com.framework.web.bus.domain.Order;
import com.framework.web.bus.domain.OrderItemInfo;
import com.framework.web.bus.repository.GoodsInfoRepository;
import com.framework.web.bus.repository.OrderItemInfoRepository;
import com.framework.web.bus.repository.OrderRepository;
import com.framework.web.bus.repository.specifications.OrderSpecs;
import com.framework.web.common.domain.BusinessConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @Title: Service
* @Description: 业务层
* @author zqtao
* @date 2016-09-11
* @version V1.0
*
*/
@Service
public class OrderService {

@Autowired
private OrderRepository  orderRepository;

@Autowired
private OrderItemInfoRepository orderItemInfoRepository;


@Autowired
private GoodsInfoRepository goodsInfoRepository;

/**
* 新增或者保存
* @param order
* @return
*/
public Order save(Order order) {
    return orderRepository.save(order);
}

/**
* 删除
* @param id
*/
public void  delete(String id) {
    orderRepository.delete(id);
}
/**
* 根据id查询
* @param id
* @return
*/
public Order findById(String id) {
    return orderRepository.findOne(id);
}


/**
* 分页查找
* @param order
* @param pr
* @return
*/
public Page<Order> getPageList(Order order, PageRequest pr) {
    Page<Order> totalList = orderRepository.findAll(OrderSpecs.pageQueryOrder(order), pr);
    return totalList;
}

/**
     * 根据订单状态获取用户订单信息
     * @param orderStatus
     * @return
     */
    public List<Order> findByOrderStatusAndUserId(Integer orderStatus,String userId){
     return orderRepository.findByOrderStatusAndUserId(orderStatus,userId);
}

    /**
     * 根据订单Id和订单状态查询订单信息
     * @param orderId
     * @param status
     * @returnString
     */
    public Order findByOrderAndStatus( String orderId,Integer status){
       return  orderRepository.findByIdAndOrderStatus(orderId, status);
    }


    @Transactional
    public Object payOrder(String orderId,Double actualPay,Double shoudlePay){
        Order oder = orderRepository.findByIdAndOrderStatus(orderId, BusinessConstants.ORDER_STATUS.NON_PAYMENT);
        Double totalAmount = oder.getTotalAmount();
        if(shoudlePay.equals(totalAmount)){
            List<OrderItemInfo> orderItemInfos = orderItemInfoRepository.findByOrderId(orderId);
            for(OrderItemInfo orderItemInfo : orderItemInfos){
                GoodsInfo goodsInfo = goodsInfoRepository.findOne(orderItemInfo.getGoodsId());
                if(goodsInfo.getStock()>=orderItemInfo.getQuantities()){
                    Integer salesVolume = goodsInfo.getSalesVolume();
                    goodsInfo.setSalesVolume(orderItemInfo.getQuantities()+salesVolume);
                    goodsInfo.setStock(goodsInfo.getStock() - orderItemInfo.getQuantities());
                    goodsInfo.setBuyers(goodsInfo.getBuyers()+1);//
                    goodsInfoRepository.save(goodsInfo);
                }else {
                    return BusinessConstants.PAY_STATUS.ORDER_FAIL_CODE;
                }
            }
            oder.setOrderStatus(BusinessConstants.ORDER_STATUS.NON_SHIPMENTS);
            orderRepository.save(oder);
            return BusinessConstants.PAY_STATUS.ORDER_SUCCESS_CODE;
        }else{
            return BusinessConstants.PAY_STATUS.ORDER_FAIL_CODE;
        }

    }


}
